MongoDB Schema Validation

You can validate schema during forward engineering scripts. The schema validator verifies fields in collections based on their data type. If enabled, this feature restricts forward engineering script generation in case the data in the fields is not in line with the assigned data type. Thus, enabling you to avoid any errors in the forward engineering script. This feature is enabled by default for fields in collections.

In case of any exceptions, you can select the Exclude Schema Validator check box to exclude validations for one or more fields. Doing so overrides the assigned data type of a field and generates the script successfully.

This topic walks you through the steps to generate schema validation for fields in a simple table (collection) and generate schema using an example. The following table lists the fields and assigned data types in a MongoDB collection, Customer.

Fields

Data Type

Name STRING
Phone INTEGER
Subjects STRING
Address OBJECT
Street STRING
City STRING

Year

INTEGER

The following image displays the fields in a collection and the assigned data types.

To generate schema validation for a MongoDB collection, follow these steps:

  1. In erwin DM, create a collection with fields, and assign relevant datatypes.

    Ensure that a database in assigned to the collection.

  2. Right-click the collection and click Field Properties.
    The MongoDB Field Editor appears.
  3. Select a filed to set schema validation and other field properties.
    Use the following options under the General tab to configure schema validations for a field:
    Exclude Schema Validator

    Use this option to exclude any validations set under Schema Validator Rule options. Selecting this option disables the Schema Validator Rule options.

    Schema Validator Rule

    Use this option to set various validation rules for a filed. These options available under this section differ based on field's data type. For more information on schema validation rule options, refer to Defining MongoDB Fields topic.

  4. Under Schema Validator Rule section, set the required validation parameters and click Close.
  5. Right-click the collection and click Collection Properties.
    The MongoDB Collection Editor appears
  6. Go to the Schema Validator tab.
  7. Click to generate schema validation template.
    This displays the generated schema validation template based on the field properties and assigned datatypes.
  8. Select Validation Action and Validation Level options. For more information about these options, refer to Defining MongoDB Collections topic.
  9. Click to edit the schema.
    The Collection Schema Validator editor appears. This displays the generated schema validation template with validations in edit mode.
  10. Edit the schema template for validations and add a description for the fields based on your requirement.

    In this example, the validation properties of the fields and descriptions are updated as shown in the following image.

  11. Once you have made changes to the schema template, click OK.
    The schema template validation changes are saved.

  12. On the application ribbon, click Action > Schema.
    The Forward Engineer Schema Generation Wizard appears.

  13. Go to Preview tab.
    The updated schema with the validations appear.

  14. In the Preview section, edit the schema to correct the random integer values.

    Generating schema validation template adds random integer values to the fields with INTEGER datatype. Ensure that you replace this with valid value to generate the schema successfully.

  15. Click Generate.
    The MongoDB Connection screen appears. Connect to the database. For more information, refer to the Database Connection Parameters topic.
    The schema is generated successfully along with the validations.

The schema generation wizard displays errors if the field-level validations are not met. For example, the schema in the following image accepts values between 1990 and 2022 in the Year field, and Name field is set as mandatory. The, Year field is updated with the value 1981, and Name field is blank.

When you click Generate, the schema generation fails as the validation rules are not met and displays the following error.

Alternatively, you can exclude validations for one or more fields to generate schema. This overrides the assigned data validation rule for a filed and generates the script successfully.

To exclude schema validation for a field, open Collection Field Editor, select a field, and then, select the Exclude Schema Validator check box.

In the Forward Engineering Schema Wizard, when you click Generate, the schema is generated successfully.